package searchSys;

import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;


public class TabFolderTabItem {

  public static void main(String[] args) {
	
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(4,false));
    shell.setSize(600, 400);
    final ToolBar bar = new ToolBar(shell,SWT.HORIZONTAL);
    final Label searchTxt = new Label(shell, SWT.NONE);
    final Text search = new Text(shell, SWT.BORDER);
    search.setSize(100, 1000);
    searchTxt.setText("Search:");
    searchTxt.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));
    
    search.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    search.setSize(100, 1000);
    
    for (int i = 0; i < 8; i++) {
      final ToolItem item = new ToolItem(bar,SWT.DROP_DOWN | SWT.NO_TRIM);
      item.setText("Item " + i); 
      
      

    }
    
    bar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1));
    final Button b = new Button(shell, SWT.PUSH);
    b.setText("Go");
    b.setToolTipText("Stop Syncronize");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));
    final Combo combo = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER);
    String[] arr = new String[] {"A","B","C"};
    combo.setItems(arr);
    combo.setText("Search By");
    combo.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));

    Menu m = new Menu(shell,SWT.BAR);

    // create a file menu and add an exit item
    final MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    final Menu filemenu = new Menu(shell, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    final MenuItem openMenuItem = new MenuItem(filemenu, SWT.PUSH);
    openMenuItem.setText("&Open\tCTRL+O");
    openMenuItem.setAccelerator(SWT.CTRL+'O');
    final MenuItem saveMenuItem = new MenuItem(filemenu, SWT.PUSH);
    saveMenuItem.setText("&Save\tCTRL+S");
    saveMenuItem.setAccelerator(SWT.CTRL+'S');
    final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
    final MenuItem exitMenuItem = new MenuItem(filemenu, SWT.PUSH);
    exitMenuItem.setText("Exit");
    
    // create an edit menu and add cut copy and paste items
    final MenuItem edit = new MenuItem(m, SWT.CASCADE);
    edit.setText("Edit");
    final Menu editmenu = new Menu(shell, SWT.DROP_DOWN);
    edit.setMenu(editmenu);
    final MenuItem cutMenuItem = new MenuItem(editmenu, SWT.PUSH);
    cutMenuItem.setText("Cut");
    final MenuItem copyMenuItem = new MenuItem(editmenu, SWT.PUSH);
    copyMenuItem.setText("Copy");
    final MenuItem pasteMenuItem = new MenuItem(editmenu, SWT.PUSH);
    pasteMenuItem.setText("Paste");            
  
    //create a Window menu and add Child item
    final MenuItem window = new MenuItem(m, SWT.CASCADE);
    window.setText("Window");
    final Menu windowmenu = new Menu(shell, SWT.DROP_DOWN);
    window.setMenu(windowmenu);
    final MenuItem maxMenuItem = new MenuItem(windowmenu, SWT.PUSH);
    maxMenuItem.setText("Maximize");
    final MenuItem minMenuItem = new MenuItem(windowmenu, SWT.PUSH);
    minMenuItem.setText("Minimize");
    
    // create a Help menu and add an about item
    final MenuItem help = new MenuItem(m, SWT.CASCADE);
    help.setText("Help");
    final Menu helpmenu = new Menu(shell, SWT.DROP_DOWN);
    help.setMenu(helpmenu);
    final MenuItem aboutMenuItem = new MenuItem(helpmenu, SWT.PUSH);
    aboutMenuItem.setText("About"); 
    
	    final CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
	    folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
	    folder.setSimple(false);
	   
	    CTabItem item = new CTabItem(folder, SWT.CLOSE);
	    item.setText("Item 1");
	    Text text = new Text(folder, SWT.MULTI);
	    text.setText("Content for Item 1");
	    
	    item.setControl(text);
	    
	    b.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
				// TODO Auto-generated method stub
				if (arg0.widget==b&&search.getText()!=""){
					openNewTab(folder, combo,search.getText());
					search.setText("");
				}
			}
		});
    int colorCount = 3; 
    Color[] colors = new Color[colorCount]; 
    colors[0] = display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND); 
    colors[1] = display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT); 
    colors[2] = colors[0]; 
    int[] percents = new int[colorCount - 1]; 
    percents[0] = 4; 
    percents[1] = 60; 
    folder.setSelectionBackground(colors, percents, true); 
    folder.setSelectionForeground(display.getSystemColor(SWT.COLOR_TITLE_FOREGROUND));
	
	
	folder.setSelection(item);
	
	KeyAdapter keyAdapter = new KeyAdapter() {
    	public void keyReleased(KeyEvent event){
    		String line = search.getText();
    		if (event.keyCode=='\r'&&line!=""){
    			// update the main screen
    			
    			if (combo.getSelectionIndex()>-1){
    				openNewTab(folder,combo,line);
    				search.setText("");
    			}
    		}	
    		
    	}
	};
	search.addKeyListener(keyAdapter);
	////////////////////////////////////// Table ////////////////////////////
	
    final Table table = new Table(folder, SWT.BORDER | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    TableItem item2 = new TableItem(table, SWT.NONE);
    item2.setText(new String[] { "a", "3" });
    item2 = new TableItem(table, SWT.NONE);
    item2.setText(new String[] { "b", "2" });
    item2 = new TableItem(table, SWT.NONE);
    item2.setText(new String[] { "c", "1" });
    column1.setWidth(100);
    column2.setWidth(100);
    Listener sortListener = new Listener() {
      public void handleEvent(Event e) {
        TableItem[] items = table.getItems();
        Collator collator = Collator.getInstance(Locale.getDefault());
        TableColumn column = (TableColumn) e.widget;
        int index = column == column1 ? 0 : 1;
        int dir = table.getSortDirection();
        TableColumn sortColumn = table.getSortColumn();
        if (sortColumn == column) {
            dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
        } else {
            table.setSortColumn(column);
            dir = SWT.UP;
        }
        
        for (int i = 1; i < items.length; i++) {
          String value1 = items[i].getText(index);
          for (int j = 0; j < i; j++) {
            String value2 = items[j].getText(index);
            if (dir==SWT.UP){
	            if (collator.compare(value1, value2) < 0) {
	              String[] values = { items[i].getText(0), items[i].getText(1) };
	              items[i].dispose();
	              TableItem item = new TableItem(table, SWT.NONE, j);
	              item.setText(values);
	              items = table.getItems();
	              break;
	            }
            }
            else{
            	if (collator.compare(value2, value1) < 0) {
  	              String[] values = { items[i].getText(0), items[i].getText(1) };
  	              items[i].dispose();
  	              TableItem item = new TableItem(table, SWT.NONE, j);
  	              item.setText(values);
  	              items = table.getItems();
  	              break;
  	            }
            }
          }
        }
        table.setSortColumn(column);
        table.setSortDirection(dir);
      }
    };
    column1.addListener(SWT.Selection, sortListener);
    column2.addListener(SWT.Selection, sortListener);  

    table.setSortColumn(column1);
    table.setSortDirection(SWT.UP);
    
    table.addMouseListener(new MouseListener() {
		
		@Override
		public void mouseUp(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void mouseDown(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void mouseDoubleClick(MouseEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println(table.getSelection()[0].getText(0)+","+table.getSelection()[0].getText(1));
		}
	});
    ////////////////////////////////////////////////////////
    item.setControl(table);
	shell.setMenuBar(m);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
  
  public static void openNewTab(final CTabFolder folder,Combo combo,String line){
	  CTabItem item2 = new CTabItem(folder, SWT.CLOSE);
	  item2.setText(line);
	  Text text = new Text(folder, SWT.MULTI);
	  text.setText("Search for " + line + "in category " + combo.getItem(combo.getSelectionIndex())+".");
	  item2.setControl(text);	
	  item2.getControl().setFocus();
	  folder.setSelection(item2);  
  }
}

